package com.dnui.seven2.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dnui.seven2.common.R;
import com.dnui.seven2.entity.Collection;
import com.dnui.seven2.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/collection")
public class CollectionController {
    @Autowired
    private CollectionService collectionService;


    @PostMapping("saveCollection")
    public R<String> saveCollection(@RequestBody Collection collection){
        collectionService.save(collection);
        return  R.success( "添加成功");
    }

    @DeleteMapping("deleteCollection/{collectionId}")
    public R<String> deleteCollection( @PathVariable Long collectionId){
        collectionService.deleteCollection(collectionId);
        return  R.success("删除成功");
    }

    @PutMapping("updateCollection")
    public R<String> updateCollection(@RequestBody Collection collection){
        collectionService.updateCollection(collection);
        return  R.success("修改成功");
    }

    @GetMapping("getById/{CollectionId}")
    public R<Collection> getById(@PathVariable Long CollectionId){
        Collection collection= collectionService.queryById(CollectionId);
        return  R.success(collection);
    }

    @GetMapping("/queryCollections")
    public R<Page> queryCollections(Integer currentPageNum, Integer everyPage ){
        //构造分页构造器
        Page pageInfo=new Page(currentPageNum,everyPage);
        //构造条件构造器
        LambdaQueryWrapper<Collection> queryWrapper=new LambdaQueryWrapper();

        //执行查询
        collectionService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    @GetMapping("/selectAll")
    public R<List<Collection>>selectAll(){
        List<Collection> collections = collectionService.selectAll();
        return R.success(collections);
    }
}
